package com.dw.article.controller;



import com.dw.article.service.ArticleUserService;
import com.dw.common.entitys.BaseResponse;
import com.dw.common.utils.ResultUtils;
import com.dw.model.article.dtos.label.AddLabelDTO;
import com.dw.model.article.dtos.label.AddLabelHotDTO;
import com.dw.model.article.dtos.label.QueryLabelDTO;
import com.dw.model.article.entitys.LabelInfo;
import com.dw.article.service.LabelInfoService;
import com.dw.model.article.vos.label.QueryLabelVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文章标签表(LabelInfo)表控制层
 *
 * @author makejava
 * @since 2024-10-08 15:20:03
 */
@Slf4j
@RestController
@RequestMapping("/article/label")
public class LabelInfoController {

    @Resource
    private LabelInfoService labelInfoService;

    @Resource
    private ArticleUserService articleUserService;

    @PostMapping("/add")
    public BaseResponse<Boolean> addLabel(@RequestBody AddLabelDTO addLabelDTO) {
        log.info("新增标签参数:{}", addLabelDTO);
        Boolean result = labelInfoService.addLabel(addLabelDTO);
        return ResultUtils.success(result);
    }

    /**
     * 条件分页查询标签
     */
    @PostMapping("/query")
    public BaseResponse<List<QueryLabelVO>> queryLabel(@RequestBody QueryLabelDTO queryLabelDTO) {
        log.info("查询标签参数:{}", queryLabelDTO);
        List<LabelInfo> labelInfos = labelInfoService.queryLabel(queryLabelDTO);
        // 类型转换
        List<QueryLabelVO> collect = labelInfos.stream().map(item -> {
            QueryLabelVO queryLabelVO = new QueryLabelVO();
            queryLabelVO.setLeiId(String.valueOf(item.getLeiId()));
            queryLabelVO.setLabelName(item.getLabelName());
            return queryLabelVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }

    /**
     * 根据标签id列表查询标签信息
     */
    @PostMapping("/queryByIds")
    public BaseResponse<List<QueryLabelVO>> queryLabelByIds(@RequestBody QueryLabelDTO queryLabelDTO) {
        log.info("根据标签id列表查询标签信息:{}", queryLabelDTO.getLabelIds());
        List<LabelInfo> labelInfos = labelInfoService.queryLabelByIds(queryLabelDTO.getLabelIds());
        // 类型转换
        List<QueryLabelVO> collect = labelInfos.stream().filter(Objects::nonNull).map(item -> {
            QueryLabelVO queryLabelVO = new QueryLabelVO();
            BeanUtils.copyProperties(item, queryLabelVO);
            return queryLabelVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }

    /**
     * 查询热门标签
     */
    @GetMapping("/queryHot")
    public BaseResponse<List<QueryLabelVO>> queryHotLabel() {
        log.info("查询热门标签");
        List<LabelInfo> labelInfos = articleUserService.queryHotLabel();
        // 类型转换
        List<QueryLabelVO> collect = labelInfos.stream().filter(Objects::nonNull).map(item -> {
            QueryLabelVO queryLabelVO = new QueryLabelVO();
            queryLabelVO.setLeiId(String.valueOf(item.getLeiId()));
            queryLabelVO.setLabelName(item.getLabelName());
            queryLabelVO.setLabelFire(item.getLabelFire());
            return queryLabelVO;
        }).collect(Collectors.toList());
        return ResultUtils.success(collect);
    }
}

